home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / mxlibs / dwstk102 / playdwm.bas < prev    next >
BASIC Source File  |  1995-04-12  |  6KB  |  213 lines

  1. '******************************************************************************
  2. 'File:      playdwm.bas
  3. 'Version:   1.02
  4. 'Tab stops: every 2 columns
  5. 'Project:   DWD Player
  6. 'Copyright: 1994-1995 DiamondWare, Ltd.  All rights reserved.
  7. 'Written:   Erik Lorenzen & Don Lemmons
  8. 'Purpose:   Contains simple example code to show how to load/play a .DWM file
  9. 'History:   94/10/21 KW Started playdwm.c
  10. '           94/11/12 DL Translated to BASIC
  11. '           95/01/12 EL Cleaned up & Finalized
  12. '           95/03/22 EL Finalized for 1.01
  13. '           95/04/11 EL Finalized for 1.02
  14. '
  15. 'Notes
  16. '-----
  17. 'This code isn't really robust when it comes to standard error checking
  18. 'and particularly recovery, software engineering technique, etc.  A buffer
  19. 'is statically allocated.  A better technique would be to use fstat() or stat()
  20. 'to determine the file's size then malloc(size).    The STK will handle songs
  21. 'larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  22. 'such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  23. 'exitting and cleanup is not handled robustly in this code.  The code below can
  24. 'only be validated by extremely careful scrutiny to make sure each case is
  25. 'handled properly.  A better method would the use of C's atexit function.
  26. '
  27. 'But all such code would make this example file less clear; its purpose was
  28. 'to illustrate how to call the STK, not how to write QA-proof software.
  29. '******************************************************************************/
  30.  
  31.  
  32.  
  33. '$INCLUDE: 'dws.bi'
  34. '$INCLUDE: 'dwt.bi'
  35. '$INCLUDE: 'err.bi'
  36.  
  37.  
  38.  
  39. TYPE BUFFTYP
  40.     buf AS STRING * 32767
  41. END TYPE
  42.  
  43.  
  44.  
  45. 'DECLARE VARIABLES
  46.     COMMON SHARED dov     AS dwsDETECTOVERRIDES
  47.     COMMON SHARED dres    AS dwsDETECTRESULTS
  48.     COMMON SHARED ideal AS dwsIDEAL
  49.     COMMON SHARED mplay AS dwsMPLAY
  50.  
  51.  
  52.  
  53. DIM SHARED buffer(0) AS BUFFTYP 'set aside string area for song to load into
  54.                                                                 'by doing it this way we give QBasic the
  55.                                                                 'opportunity to place the song into far mem
  56. 'START OF MAIN
  57.  
  58.     PRINT
  59.     PRINT "PLAYDWM 1.02 is Copyright 1994-95, DiamondWare, Ltd."
  60.     PRINT "All rights reserved."
  61.     PRINT : PRINT : PRINT
  62.  
  63.     musvol%         = 255
  64.  
  65.     filename$ = LTRIM$(RTRIM$(COMMAND$))
  66.     IF filename$ = "" THEN
  67.         PRINT "Usage PLAYDWD <dwd-file>"
  68.         GOTO ProgramExit
  69.     END IF
  70.  
  71.     OPEN filename$ FOR BINARY AS #1 LEN = 1
  72.     filelen = LOF(1)
  73.     CLOSE #1
  74.  
  75.     IF filelen = 0 THEN
  76.         PRINT "File Not Found"
  77.         GOTO ProgramExit
  78.     END IF
  79.  
  80.     IF filelen > 32767 THEN
  81.         PRINT "File Too Big"
  82.         GOTO ProgramExit
  83.     END IF
  84.  
  85.     OPEN filename$ FOR BINARY AS #1 LEN = 1
  86.     GET #1, 1, buffer(0).buf
  87.     CLOSE #1
  88.  
  89.     'We need to set every field to -1 in dwsDETECTOVERRIDES struct; this
  90.     'tells the STK to autodetect everything.  Any other value
  91.     'overrides the autodetect routine, and will be accepted on
  92.     'faith, though the STK will verify it if possible.
  93.  
  94.     dov.baseport = -1
  95.     dov.digdma     = -1
  96.     dov.digirq     = -1
  97.  
  98.     IF DWSDetectHardWare(dov, dres) = 0 THEN
  99.         errDisplay
  100.         GOTO ProgramExit
  101.     END IF
  102.  
  103.     IF (dres.capability AND dwscapabilityFM) <> dwscapabilityFM THEN
  104.         PRINT"FM support not found"
  105.         GOTO ProgramExit
  106.     END IF
  107.  
  108.     'The "ideal" struct tells the STK how you'd like it to initialize the
  109.     'sound hardware.      In all cases, if the hardware won't support your
  110.     'request, the STK will go as close as possible.  For example, not all
  111.     'sound boards will support al sampling rates (some only support 5 or
  112.     '6 discrete rates).
  113.  
  114.     ideal.musictyp     = 1                    '0=No music, 1=OPL2
  115.     ideal.digtyp         = 0                    '0=No Dig, 8=8bit, 16=16bit
  116.     ideal.digrate      = 0                    'sampling rate, in Hz
  117.     ideal.dignvoices = 0                    'number of voicws.bies (up to 16)
  118.     ideal.dignchan     = 0                    '1=mono, 2=stereo
  119.  
  120.     IF dwsInit(dres, ideal) = 0 THEN
  121.         errDisplay
  122.         GOTO ProgramKill
  123.     END IF
  124.  
  125.     'Set music vol to about 4/5ths of max
  126.     musvol% = 200
  127.     IF dwsXMusic(musvol%) = 0 THEN
  128.         errDisplay
  129.     END IF
  130.  
  131.     '72.8Hz is a decent compromise.  It will work in a Windows DOS box
  132.     'without any problems, and yet it allows music to sound pretty good.
  133.     'In my opinion, there's no reason to go lower than 72.8 (unless you
  134.     'don't want the hardware timer reprogrammed)--music sounds kinda chunky
  135.     'at lower rates.  You can go to 145.6 Hz, and get smoother (very
  136.     'subtly) sounding music, at the cost that it will NOT run at the correct
  137.     '(or constant) speed in a Windows DOS box.
  138.  
  139.     dwtInit(dwt728HZ)
  140.  
  141.     soundseg% = VARSEG(buffer(0).buf)
  142.     soundoff% = VARPTR(buffer(0).buf)
  143.     pointer&    = soundseg% * 256 ^ 2 + soundoff%  'make pointer
  144.  
  145.     mplay.track = pointer&
  146.     mplay.count = 1                  '0=infinite loop, 1-N num times to play sound
  147.  
  148.     IF dwsMPlay(mplay) = 0 THEN
  149.         errDisplay
  150.         GOTO ProgramKill
  151.     END IF
  152.  
  153.     'We're playing.  Let's exit when the song is over, and allow the user
  154.     'to fiddle with the volume level (mixer) in the meantime
  155.  
  156.     PRINT"Press + or - to change playback volume"
  157.  
  158.     result% = dwsMSONGSTATUSPLAYING
  159.     DO UNTIL (result%  AND dwsMSONGSTATUSPLAYING) <> dwsMSONGSTATUSPLAYING
  160.         inpt$ = INKEY$
  161.  
  162.         IF inpt$ = "+" THEN
  163.             musvol% = musvol% + 1
  164.  
  165.             PRINT"Music Volume is ";musvol%
  166.  
  167.             IF dwsXMusic(musvol%) = 0 THEN
  168.                 errDisplay
  169.             END IF
  170.         END IF
  171.  
  172.         IF inpt$ = "-" THEN
  173.             musvol% = musvol% - 1
  174.  
  175.             PRINT"Music Volume is ";musvol%
  176.  
  177.             IF dwsXMusic(musvol%) = 0 THEN
  178.                 errDisplay
  179.             END IF
  180.         END IF
  181.  
  182.         IF inpt$ = "q" OR inpt$ = "q" OR inpt$ = chr$(27) THEN
  183.             GOTO ProgramKill
  184.         END IF
  185.  
  186.         IF dwsMSongStatus(result%) = 0 THEN
  187.             errDisplay
  188.             GOTO ProgramKill
  189.         END IF
  190.     LOOP
  191.  
  192.     ProgramKill:
  193.  
  194.     'If dwt is not inited calling dwt_Kill will have no effect
  195.     dwtKill
  196.  
  197.     IF dwsKill = 0 THEN
  198.         errnum = dwsErrNo
  199.         errDisplay
  200.  
  201.         'If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  202.         'or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  203.         'must remove his tsr, and dws_Kill must be called again.  If it's
  204.         'dws_NOTINITTED, there's nothing to worry about at this point.
  205.         IF errnum = dwsKillCANTUNHOOKISR THEN
  206.             GOTO ProgramKill
  207.         END IF
  208.     END IF
  209.  
  210.     ProgramExit:
  211.  
  212. END
  213.